package com.moss.cloud.common.sms.service.imp;

import com.moss.cloud.common.sms.config.SmsConfig;
import com.moss.cloud.common.sms.exception.SmsErrorType;
import com.moss.cloud.common.sms.exception.SmsException;
import com.moss.cloud.common.sms.factory.SmsFactory;
import com.moss.cloud.common.sms.model.SmsDTO;
import com.moss.cloud.common.sms.model.SmsModel;
import com.moss.cloud.common.sms.operation.SmsOperation;
import com.moss.cloud.common.sms.service.ISmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class SmsServiceImpl implements ISmsService {

    private final SmsConfig smsConfig;

    public SmsServiceImpl(SmsConfig smsConfig) {
        this.smsConfig = smsConfig;
    }

    /**
     * 通用文件上传
     *
     * @param smsDTO
     * @return
     */
    @Override
    public void sendSms(SmsDTO smsDTO) {
        SmsModel smsModel = SmsModel.builder().phoneNumbers(smsDTO.getPhoneNumbers())
                .sign(smsDTO.getSign())
                .templateID(smsDTO.getTemplateID())
                .templateParams(smsDTO.getTemplateParams()).smsConfig(smsConfig).build();
        SmsOperation operation = SmsFactory.smsType(smsConfig.getType()).orElseThrow(() ->
                new SmsException(SmsErrorType.STORAGE_ERROR));
        operation.sendSms(smsModel);
    }

}
